<?php

/**
 * 后台首页小图模块 Controller
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zhangwentao $  <zwttmac@qq.com>
 * $Id: commentAction.php 1402 2011-03-03 10:15:34Z zhangwentao $
 * http://www.t-mac.org；
 */
class commentAction extends Action
{

    private $tmp_model;

    /**
     * _init 方法 在执行任何Action前执行
     */
    public function _init()
    {
        $this->assign('action', $_GET['TMAC_ACTION']);
        $this->tmp_model = Tmac::model('comment');
        $check_model = $this->M('check');
        $check_model->checkLogin();
        $check_model->CheckPurview('tb_admin,tb_user');
    }

    /**
     * 资讯类别管理 首页
     */
    public function index()
    {        
        $comment_approved = $this->getParam('comment_approved');
        $comment_approved = !empty($comment_approved) ? $comment_approved : 'none';
        $search_keyword = $this->H($this->getParam('search_keyword'));
        $comment_do = $this->getParam('comment_do');
        $uid = $_SESSION['admin_uid'];
        
        //取友情链接类型radiobutton数组
        $comment_type_ary = UtilityConfig::comment_type_ary();
        $comment_type_ary_option = Utility::Option($comment_type_ary, $comment_approved);

        //取友情操作类型radiobutton数组
        $comment_do_ary = UtilityConfig::comment_do_ary();
        $comment_do_ary_option = Utility::Option($comment_do_ary, $comment_do);

        //TODO  取出所有评论
        $rs = $this->tmp_model->getCommentList('', $comment_approved, $search_keyword, $uid);
        $this->assign('comment_type_ary_option', $comment_type_ary_option);
        $this->assign('comment_do_ary_option', $comment_do_ary_option);

        $this->V('comment');
    }

    /**
     * 新增/修改首页小图
     */
    public function add()
    {
        $comment_id = $this->getParam('comment_id');
        if ($comment_id > 0) {
            $editinfo = $this->tmp_model->getcommentInfo($comment_id);
        } else {
            $editinfo['title'] = '';
            $editinfo['comment_author'] = '';
            $editinfo['comment_author_email'] = '';
            $editinfo['comment_author_url'] = '';
            $editinfo['comment_content'] = '';
        }

        $this->assign('editinfo', $editinfo);

        $this->V('comment');
    }

    /**
     * 新增/修改首页小图页面　保存　
     */
    public function save()
    {
        if (empty($_POST) || count($_POST) < 4) {
            $this->redirect('don\'t be evil');
            exit;
        }

        /* 初始化变量 */
        $comment_id = !empty($_POST['comment_id']) ? (int) $_POST['comment_id'] : 0;
        $comment_author = !empty($_POST['comment_author']) ? trim($this->H($_POST['comment_author'])) : '';
        $comment_author_email = !empty($_POST['comment_author_email']) ? trim($this->H($_POST['comment_author_email'])) : '';
        $comment_author_url = !empty($_POST['comment_author_url']) ? $_POST['comment_author_url'] : '';
        $comment_content = !empty($_POST['comment_content']) ? $this->H($_POST['comment_content']) : '0';

        /* 验证不能为空 */
        if (empty($comment_author)) {
            $this->redirect('评论作者不能为空');
            exit;
        }
        if (empty($comment_author_email)) {
            $this->redirect('评论作者email不能为空');
            exit;
        }
        if (!isset($comment_content)) {
            $this->redirect('评论内容不能为!');
            exit;
        }
        // TODO goon to verify

        $info = array(
            'comment_author' => $comment_author,
            'comment_author_email' => $comment_author_email,
            'comment_author_url' => $comment_author_url,
            'comment_content' => $comment_content
        );

        if ($comment_id > 0) {
            $comment_page = HttpResponse::getCookie('comment_page');
            //update save article
            $where = 'comment_id = ' . $comment_id . '';
            $rs = $this->tmp_model->addcomment($info, 'UPDATE', $where);
            if ($rs) {
                $this->redirect('修改评论成功', PHP_SELF . '?m=comment&page=' . $comment_page . '');
            } else {
                $this->redirect('修改评论失败');
            }
        } else {
            //insert save article_class
            $rs = $this->tmp_model->addcomment($info);
            if ($rs) {
                $this->redirect('添加评论成功', PHP_SELF . '?m=comment');
            } else {
                $this->redirect('添加评论失败');
            }
        }
    }

    public function comment_do()
    {
        $comment_do = $this->getParam('comment_do');
        $id_a = $this->getParam('id_a');
        if ($comment_do == 'del') {
            $this->del();
        } else {
            if (is_array($id_a)) {
                $id = implode(',', $id_a);
            } elseif (!empty($do_id)) {
                $id = $do_id;
            } else {
                $this->redirect('请选择要操作的评论...');
                exit;
            }
            if ($comment_do == 'hidden') {
                $comment_approved = 0;
                $rs_info = '隐藏选定的';
            } elseif ($comment_do == 'display') {
                $comment_approved = 1;
                $rs_info = '显示选定的';
            } elseif ($comment_do == 'spam') {
                $comment_approved = 'spam';
                $rs_info = '标记选定的为垃圾';
            } else {
                $rs_info = '';
            }
            $info = array(
                'comment_approved' => $comment_approved
            );
            //update save article
            $where = 'comment_id in (' . $id . ')';
            $rs = $this->tmp_model->addcomment($info, 'UPDATE', $where);
            // TODO 处理完
            if ($rs) {
//                $comment_page = HttpResponse::getCookie('comment_page');
                $this->redirect($rs_info . '评论成功');
            } else {
                $this->redirect($rs_info . '评论失败，请重试！');
            }
        }
    }

    /**
     * del
     * @param int $class_id
     */
    public function del()
    {
        $do_id = $this->getParam('comment_id');
        $id_a = !empty($_POST['id_a']) ? $_POST['id_a'] : '';
        if (is_array($id_a)) {
            $id = implode(',', $id_a);
        } elseif (!empty($do_id)) {
            $id = $do_id;
        } else {
            $this->redirect('请选择要删除的...');
            exit;
        }
        $rs = $this->tmp_model->delcomment($id);
        // TODO DEL该分类下的所有资讯
        if ($rs) {
            $comment_page = HttpResponse::getCookie('comment_page');
            $this->redirect('删除评论成功', PHP_SELF . '?m=comment&page=' . $comment_page . '');
        } else {
            $this->redirect('删除评论失败，请重试！');
        }
    }

    public function changeCommentType()
    {
        $comment_id = !empty($_POST['comment_id']) ? (int) $_POST['comment_id'] : 0;
        $type = !empty($_POST['type']) ? (int) $_POST['type'] : 0;

        if ($type == 0) {
            $comment_approved = 1;
        } elseif ($type == 1) {
            $comment_approved = 0;
        } else {
            $comment_approved = 0;
        }
        $info = array(
            'comment_approved' => $comment_approved
        );
        //update save article
        $where = 'comment_id = ' . $comment_id . '';
        $rs = $this->tmp_model->addcomment($info, 'UPDATE', $where);
        if ($rs) {
            if ($comment_approved == 1) {

                $data = "<a class='yes' title='点击变为隐藏状态' onclick=\"changeCommentType($comment_id,$comment_approved)\" style='cursor:pointer'>显示</a>";
            } elseif ($comment_approved == 0) {
                $data = "<a class='no' title='点击变为显示状态' onclick=\"changeCommentType($comment_id,$comment_approved)\" style='cursor:pointer'>隐藏</a>";
            }
            $error = 0;
        } else {
            $error = 1;
            $data = '';
        }
        $data = array('id' => $comment_id, 'data' => $data, 'error' => $error);
        $rs = json_encode($data);
        exit($rs);
    }

    /**
     * 查看留言
     */
    public function show()
    {
        $comment_id = intval($this->getParam('comment_id'));
        $page = HttpResponse::getCookie('comment_page');
        
        if (empty($comment_id)) {
            $this->redirect('留言id不能为空');
            exit;
        }
        $editinfo = $this->tmp_model->getcommentInfo($comment_id);
        $this->assign('editinfo', $editinfo);
        $this->assign('page', $page);
        $this->V('comment');
    }    
}